package com.elong.businesstravel.modules.help;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.support.jhf.utils.AppJump;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.activity.BaseActivity;
import com.elong.businesstravel.base.activity.BaseWebViewActivity;
import com.elong.businesstravel.base.umeng.feedback.ConversationActivity;
import com.elong.businesstravel.base.utils.AppUserId;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.include.Version;
import com.elong.businesstravel.modules.login.LoginActivity;
import com.elong.businesstravel.protocol.ProtocolDef;

/**
 * 常见问题
 * 
 * @author lenovo
 * 
 */
public class HelpActivity extends BaseWebViewActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_help);

		init();
	}

	@Override
	protected void init() {

		mWebView = (WebView) findViewById(R.id.webView);
		initWebView(mWebView);
		// 每次都在网络上获取
		mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

		String useridString = AppUserId.getUserId(mContext);
		if (TextUtils.isEmpty(useridString)) {
			useridString = "0";
		}
		String urlString = ProtocolDef.HELP_URL + "?" + "uid=" + useridString
				+ "&deviceid=" + AppUserId.getDeviceId(mContext) + "&uuid="
				+ AppUserId.getUUId(mContext) + "&version="
				+ Version.versionName;
		mWebView.loadUrl(urlString);

		setListener();

		setTitleNavBar();
	}

	@Override
	protected void setListener() {
		mWebView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url.equals(ProtocolDef.HELP_FEED_BACK_URL)) {
					goConversationActivity();
				} else if (url.equals(ProtocolDef.HELP_GUESS_HOTEL_URL)) {
					// 点击活动按钮
					finish();
				} else {
					view.loadUrl(url);
				}
				return true;
			}

			public void onPageFinished(WebView view, String url) {

			}

			public void onPageStarted(WebView view, String url, Bitmap favicon) {

			}
		});
	}

	@Override
	protected void setTitleNavBar() {

		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("常见问题");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				cancel();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	/**
	 * 跳转到反馈界面
	 */
	private void goConversationActivity() {
		if (TextUtils.isEmpty(AppUserId.getUserId(mContext))) {
			// 登陆
			// Intent intent = new Intent(mContext, WXEntryActivity.class);
			Intent intent = new Intent(mContext, LoginActivity.class);
			startActivityForResult(intent, 0);
		} else {
			// 反馈
			Intent intent = new Intent(mContext, ConversationActivity.class);
			startActivity(intent);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 登陆返回
			switch (resultCode) {
			case RESULT_OK: {
				goConversationActivity();
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

}
